<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Graph demo</title>

    <style type="text/css">
        body {font: 10pt arial;}
    </style>

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript" src="../graph.js"></script>
    <!--[if IE]><script type="text/javascript" src="../excanvas.js"></script><![endif]-->
    <link rel="stylesheet" type="text/css" href="../graph.css">

    <script type="text/javascript">
        var graph = null;
        var data = null;
        var dateInit = new Date();
        var timerRedraw = null;
        google.load("visualization", "1");

        // Set callback to run when API is loaded
        google.setOnLoadCallback(drawVisualization);

        function customFunction(x) {
            return Math.sin(x / 1000000) * Math.cos(x / 500000) * 10;
        }

        function redraw(startDate, endDate) {
            // remove all rows
            data.removeRows(0, data.getNumberOfRows());

            var d = new Date(startDate);
            var i = 0;
            while(d < endDate) {
                data.addRow([new Date(d), customFunction(dateInit - d.getTime())]);
                d.setMinutes(d.getMinutes() + 1);
                i++;
            }

            // TODO: redraw works wrong when still moving the graph (mouse is down)
            document.title = data.getNumberOfRows() + " datapoints";
            graph.draw(data);
            graph.setValueRangeAuto();
        }

        // Called when the Visualization API is loaded.
        function drawVisualization() {
            // Create and populate a data table.
            data = new google.visualization.DataTable();
            data.addColumn('datetime', 'time');
            data.addColumn('number', 'Custom Function');

            // create data
            var d = new Date(2010, 9, 23, 20, 0, 0);
            for (var i = 0; i < 1000; i++) {
                data.addRow([new Date(d), customFunction(dateInit - d.getTime())]);
                d.setMinutes(d.getMinutes() + 1);
            }

            // specify options
            var options = {width:  "100%",
                height: "350px"
            };

            // Instantiate our graph object.
            graph = new links.Graph(document.getElementById('mygraph'));

            function onrangechanged() {
                // adjust the values of startDate and endDate
                var range = graph.getVisibleChartRange();
                redraw(range.start, range.end);
            }

            // Add event listeners
            google.visualization.events.addListener(graph, 'rangechanged', onrangechanged);

            // Draw our graph with the created data and options
            graph.draw(data, options);
        }
    </script>
</head>

<body>
<div id="mygraph"></div>

<div id="info"></div>
</body>
</html>
